_Pragma("once")

#include "Utility.h"
#include "StringUtility.h"


class ICommunicator {
public:
    /** Default constructor */
    ICommunicator();
    virtual ~ICommunicator() {
    }

    virtual bool close() noexcept {
        return true;
    }

    virtual ssize_t send(const NetworkString& data) noexcept {
        return sendRaw(data.data(), data.size());
    }


    virtual ssize_t recv(NetworkString& buffer, const size_t beginIndex = 0) noexcept {
        buffer.resize(beginIndex + MAX_BUFFER_SIZE);
        ssize_t readed = recvRaw(&buffer[beginIndex], MAX_BUFFER_SIZE);
        if (0 <= readed) {
            buffer.resize(beginIndex + readed);
        } else {
            buffer.resize(beginIndex);
        }

        return readed;
    }

    virtual ssize_t sendRaw(const void* data, size_t length) noexcept = 0;

    virtual ssize_t recvRaw(void* buffer, size_t length) noexcept = 0;


};
